// 配置路由的文件
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/home.vue"; //路径别名，直接找到src
import Job from "@/views/job.vue";
import store from "@/store"; //此处的store等同于组件中的this.$store
Vue.use(VueRouter);

//创建路由对象
const router = new VueRouter({
  // mode:'history',  //启用history模式
  routes: [
    {
      path: "/",
      component: () => import("@/views/main.vue"),
      children: [
        { path: "home", alias: "/", component: Home },
        { path: "job", component: Job },
        { path: "mine", component: () => import("@/views/mine.vue") },
      ],
    },
    { path: "/detail/:id", component: () => import("@/views/detail.vue") },
    { path: "/login", component: () => import("@/views/login.vue") },
    { path: "/join", component: () => import("@/views/join.vue") },
    {
      path: "/vuex/test",
      component: () => import("@/views/vuex-test/index.vue"),
    },
    {
      path: "/sign",
      component: () => import("@/views/contractSign.vue"),
    },
    // {
    //   path:'/xx',
    //   components:{
    //     comp1:xxx,
    //     comp2:xxx,
    //     comp3:xxx,
    //   }
    // }
  ],
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log(to, from);
  // next();
  let blackList = ["/mine"];
  if (blackList.includes(to.path)) {
    // 用户登录状态
    let { userInfo } = store.state.user;
    console.log(userInfo);
    if (userInfo) {
      next(); //已登录
    } else {
      store.commit("user/memoMut", to.path);
      next("/login"); //未登录
    }
  } else {
    next(); //不需要拦截的路由
  }
});

export default router;
